﻿#include "kbeconfig.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include "helper/log.h"

using namespace rapidjson;
using namespace std;
using namespace xpy;


enum class JsonType
{
    STRING = 1,
    BOOL = 2,
    INT = 3
};

string remove_annotations(const char *text)
{
    size_t len = strlen(text);
    char *s = new char[len + 1];
    bool line = false;
    bool block = false;
    bool quotes = false;
    int i = 0;
    int j = 0;

    while (i < len)
    {
        char c = text[i];
        if (c == '/')
        {
            if ((!quotes) && i + 2 < len)
            {
                char next = text[i + 1];
                if (next == '/')
                {
                    line = true;
                    ++i;
                }
                else if (next == '*')
                {
                    block = true;
                    ++i;
                }
            }
        }
        else if (c == '*')
        {
            if ((!quotes) && i + 2 < len)
            {
                if (text[i + 1] == '/')
                {
                    block = false;
                    i += 2;
                    continue;
                }
            }
        }
        else if (c == '"')
        {
            quotes = !quotes;
        }
        else if (c == '\n')
        {
            line = false;
        }

        if (!line && !block)
        {
            s[j++] = c;
        }

        ++i;
    }
    s[j] = '\0';
    string r = s;
    delete[] s;
    return r;
}

KBEConfig::KBEConfig()
{
}

KBEConfig::~KBEConfig()
{
}

bool KBEConfig::loadJsonStr(const char *text)
{
    logger::debug("KBEConfig::loadJsonStr(): \n{}", text);
    string json_str = remove_annotations(text); // 去掉json注释
    logger::debug("KBEConfig::loadJsonStr(): \n{}", json_str);
    Document d;
    d.Parse(json_str.c_str());

    if (d.HasMember("ip"))
    {
        const Value &s_ip = d["ip"];
        ip = s_ip.GetString();
    }
    if(d.HasMember("port"))
    {
        const Value &s_port = d["port"];
        port = s_port.GetInt();
    }
    // CLIENT_TYPE clientType = CLIENT_TYPE::CLIENT_TYPE_MINI
    if(d.HasMember("clientType"))
    {
        const Value &s_clientType = d["clientType"];
        clientType = (CLIENT_TYPE)s_clientType.GetInt();
    }
    if(d.HasMember("persistentDataPath"))
    {
        const Value &s_persistentDataPath = d["persistentDataPath"];
        persistentDataPath = s_persistentDataPath.GetString();
    }
    if(d.HasMember("syncPlayer"))
    {
        const Value &s_syncPlayer = d["syncPlayer"];
        syncPlayer = s_syncPlayer.GetBool();
    }
    if(d.HasMember("useAliasEntityID"))
    {
        const Value &s_useAliasEntityID = d["useAliasEntityID"];
        useAliasEntityID = s_useAliasEntityID.GetBool();
    }
    if(d.HasMember("isOnInitCallPropertysSetMethods"))
    {
        const Value &s_isOnInitCallPropertysSetMethods = d["isOnInitCallPropertysSetMethods"];
        isOnInitCallPropertysSetMethods = s_isOnInitCallPropertysSetMethods.GetBool();
    }
    if(d.HasMember("SEND_BUFFER_MAX"))
    {
        const Value &s_SEND_BUFFER_MAX = d["SEND_BUFFER_MAX"];
        SEND_BUFFER_MAX = s_SEND_BUFFER_MAX.GetInt();
    }
    if(d.HasMember("RECV_BUFFER_MAX"))
    {
        const Value &s_RECV_BUFFER_MAX = d["RECV_BUFFER_MAX"];
        RECV_BUFFER_MAX = s_RECV_BUFFER_MAX.GetInt();
    }
    if(d.HasMember("isMultiThreads"))
    {
        const Value &s_isMultiThreads = d["isMultiThreads"];
        isMultiThreads = s_isMultiThreads.GetBool();
    }
    if(d.HasMember("threadUpdateHZ"))
    {
        const Value &s_threadUpdateHZ = d["threadUpdateHZ"];
        threadUpdateHZ = s_threadUpdateHZ.GetInt();
    }

    return true;
}
